package com.xiaotu.spider.aiman.pageprocessor;

import java.util.Iterator;
import java.util.Map;

import com.xiaotu.common.exception.SpiderException;
import com.xiaotu.common.exception.SpiderExceptionCode;
import com.xiaotu.common.model.MovieFansDetail;
import com.xiaotu.common.model.MovieFansPeople;
import com.xiaotu.common.model.StarFansDetail;
import com.xiaotu.common.model.StarFansPeople;

import net.sf.json.JSONObject;
import us.codecraft.webmagic.Page;

/**
 * @类名 AiManMovieDto
 * @日期 2017年11月29日
 * @作者 王艳龙
 * @功能 艾漫数据转Model
 */
public class AiManModelUtils {

	public static void setAiManModel(Page page,Map<String, Object> tvInfoMap,String type){
		String[] sex=null;
		String[] sexValue=null;
		String[] areas=null;
		String[] areaValue=null;
		String[] age=null;
		String[] ageValue=null;
		String jsonStr = page.getRawText();
		jsonStr=jsonStr.replace("\\", "");
 	 	jsonStr = jsonStr.substring(jsonStr.indexOf("{\"data\""));
 	 	jsonStr = jsonStr.substring(0, jsonStr.lastIndexOf("\"}"));
	 	JSONObject C_json = JSONObject.fromObject(jsonStr);
	 	if (!jsonStr.contains("\"女\":") && !jsonStr.contains("\"男\":"))
			throw new SpiderException(
					SpiderExceptionCode.DataInvalideException);
	 	if(jsonStr.contains("fansTab") && jsonStr.contains("exponentialOther")){
			String fabPeopleJson = C_json.getJSONObject("data").getJSONObject("fansTab").getString("exponentialOther");
			fabPeopleJson = fabPeopleJson.replace("[", "");
			fabPeopleJson = fabPeopleJson.replace("]", "");
			JSONObject jsonArr = JSONObject.fromObject(fabPeopleJson);
			if(fabPeopleJson.contains("genderFans")){
				JSONObject my = new JSONObject().fromObject(jsonArr.getString("genderFans"));
				 Iterator keys = my.keys();
				 if(my.size()!=0){
					 int i = 0;
					 sex = new String[my.size()];
					 sexValue = new String[my.size()];
					 while (keys.hasNext()) {
						 String key = (String) keys.next();
						 String value = my.getString(key);
						 sex[i]=key;
						 sexValue[i]=value;
						 i++;
					 }
				 }
			}
			
			if(fabPeopleJson.contains("provinceFans")){
				JSONObject my = new JSONObject().fromObject(jsonArr.getString("provinceFans"));
				 Iterator keys = my.keys();
				 if(my.size()!=0){
					 int i = 0;
					 areas = new String[my.size()];
					 areaValue = new String[my.size()];
					 while (keys.hasNext()) {
						 String key = (String) keys.next();
						 String value = my.getString(key);
						 areas[i]=key;
						 areaValue[i]=value;
						 i++;
					 }
				 }
			}
			if(fabPeopleJson.contains("ageFans")){
				JSONObject my = new JSONObject().fromObject(jsonArr.getString("ageFans"));
				 Iterator keys = my.keys();
				 if(my.size()!=0){
					 int i = 0;
					 age = new String[my.size()];
					 ageValue = new String[my.size()];
					 while (keys.hasNext()) {
						 String key = (String) keys.next();
						 String value = my.getString(key);
						 age[i]=key;
						 ageValue[i]=value;
						 i++;
					 }
				 }
			}
			
			if(type.equals("movie")){
				page.putField(MovieFansPeople.class.getName(), new MovieFansPeople((String) tvInfoMap.get("tvid"),(String) tvInfoMap.get("tvName"),
						sex, sexValue, age, ageValue, areas, areaValue));	
			}else{
				page.putField(StarFansPeople.class.getName(), new StarFansPeople((String) tvInfoMap.get("starid"),(String) tvInfoMap.get("name"),
						sex, sexValue, age, ageValue, areas, areaValue));	
			}
		}
		if(jsonStr.contains("fansTab") && jsonStr.contains("dataTable")){
			String dataTable = C_json.getJSONObject("data").getJSONObject("fansTab").getString("dataTable");
			dataTable = dataTable.replace("[", "");
			dataTable = dataTable.replace("]", "");
			JSONObject dataTableArr = JSONObject.fromObject(dataTable);
			String fdate = null;//分析时间
			String activeValue = null;//活跃受众
			String changeValue = null;//受众变化
			String retainValue = null;//留存受众数
			String lossValue = null;//流失受众数
			String newValue = null;//新增受众数
			String loyaltyValue = null;//受众忠诚度
			String leaveValue = null;//留言原创度
			String avgLeaveValue = null;//人均留言数
			if(dataTable.contains("beginDate")){
				//时间范围
				fdate =dataTableArr.getString("beginDate");
			}
			if(dataTable.contains("user_day_num")){
				//活跃受众
				activeValue = dataTableArr.getString("user_day_num");
			}
			if(dataTable.contains("user_change_num")){
				//受众变化
				changeValue = dataTableArr.getString("user_change_num");
			}
			if(dataTable.contains("user_retained_num")){
				//留存受众数
				retainValue = dataTableArr.getString("user_retained_num");
			}
			if(dataTable.contains("user_loss_num")){
				//流失受众数
				lossValue = dataTableArr.getString("user_loss_num");
			}
			if(dataTable.contains("user_added_num")){
				//新增受众数
				newValue = dataTableArr.getString("user_added_num");
			}
			if(dataTable.contains("user_loyalty_per")){
				//受众忠诚度
				loyaltyValue = dataTableArr.getString("user_loyalty_per");
				if(loyaltyValue !=null && !loyaltyValue.isEmpty()){
					loyaltyValue = String.format("%.2f", Double.parseDouble(loyaltyValue))+"%";
				}
				
			}
			if(dataTable.contains("user_quality_per")){
				//留言原创度
				leaveValue = dataTableArr.getString("user_quality_per");
				if(leaveValue !=null && !leaveValue.isEmpty()){
					leaveValue = String.format("%.2f", Double.parseDouble(leaveValue))+"%";
				}
			}
			if(dataTable.contains("user_discuss_avg")){
				//人均留言数
				avgLeaveValue = dataTableArr.getString("user_discuss_avg");
			}
			if(type.equals("movie")){
				page.putField(MovieFansDetail.class.getName(), new MovieFansDetail((String) tvInfoMap.get("tvid"),(String) tvInfoMap.get("tvName"),
						fdate, activeValue==null?"0":activeValue, changeValue==null?"0":changeValue, retainValue==null?"0":retainValue, 
						lossValue==null?"0":lossValue, newValue==null?"0":newValue, loyaltyValue==null?"0.00%":loyaltyValue,
								leaveValue==null?"0.00%":leaveValue, avgLeaveValue==null?"0":avgLeaveValue));
				
			}else{
				page.putField(StarFansDetail.class.getName(), new StarFansDetail((String) tvInfoMap.get("starid"),(String) tvInfoMap.get("name"),
						fdate, activeValue==null?"0":activeValue, changeValue==null?"0":changeValue, retainValue==null?"0":retainValue, 
								lossValue==null?"0":lossValue, newValue==null?"0":newValue, loyaltyValue==null?"0.00%":loyaltyValue,
										leaveValue==null?"0.00%":leaveValue, avgLeaveValue==null?"0":avgLeaveValue));
			}
			
		}
	}
}
